package com.wnhz.work.yz.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2016/10/26.
 */
public class WindowsTools {
    //退出时间
    private static long exitTime = 0;

    /**
     * 再点一次退出
     *
     * @param activity 当前activity
     */
    public static void exit(Activity activity) {
        //如果两次单击时间间隔小于两秒，弹出提出窗，否则退出
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(activity, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            activity.finish();
            System.exit(0);
        }
    }

    /**
     * 设置滑块的宽度b并返回该宽度
     *
     * @param activity 当前activity
     * @param i        Fragment的数量
     * @param view     滑块
     */
    public static int getWidth(Activity activity, int i, View view) {
        DisplayMetrics display = new DisplayMetrics();
        activity.getWindow().getWindowManager().getDefaultDisplay().getMetrics(display);
        /** 界面的宽度 */
        int windowWidth = display.widthPixels;
        LinearLayout.LayoutParams lpLineView = (LinearLayout.LayoutParams) view.getLayoutParams();
        /** 标记的滑动线的宽度 */
        lpLineView.width = windowWidth / i;
        view.setLayoutParams(lpLineView);
        return windowWidth / i;
    }

    /**
     * 滑块跟随页面滑动
     *
     * @param position
     * @param positionOffset
     * @param windowWidth
     * @param view
     */
    public static void HuaDong(int position, float positionOffset, int currentItem, int windowWidth, View view) {
        LinearLayout.LayoutParams lpLineView = (LinearLayout.LayoutParams) view.getLayoutParams();
        /**
         * 利用currentItem(当前所在页面)和position(下一个页面)以及positionOffset来
         * 设置lpLineView的左边距 滑动场景：
         * 记2个页面,
         * 从左到右分别为0,1
         * 0->1; 1->0
         */
        if (currentItem == position) {//小->大
            lpLineView.leftMargin = (int) (positionOffset * (windowWidth * 1.0) + currentItem * (windowWidth * 1.0));
        } else if (currentItem - position == 1) {//大->小
            lpLineView.leftMargin = (int) ((positionOffset - 1) * (windowWidth * 1.0) + currentItem * (windowWidth * 1.0));
        }
        view.setLayoutParams(lpLineView);

    }

    /**
     * 获取图片的id
     * @param imageView
     * @return
     */
    public static int getImageId(ImageView imageView) {
        Field[] fields = imageView.getClass().getDeclaredFields();
        int imgid = 0;
        for (Field f : fields) {
            if (f.getName().equals("mResource")) {

                f.setAccessible(true);
                try {
                    imgid = f.getInt(imageView);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return imgid;
    }

    /**
     * 移除选择过的图片
     * @param linearLayout
     * @param imageView
     * @param context
     */
    public static void removePic(final LinearLayout linearLayout , final ImageView imageView, final Context context){
        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("确定移除？");
                builder.setPositiveButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(imageView!=null){
                            linearLayout.removeView(imageView);
                        }
                    }
                });
                builder.create().show();
                return true;
            }
        });
    }
}


